Constructor

Same named function of class. Ctr is called when object of class is created. Used to provide values to data members.
Return type: None
Cannot access to address of ctr
Inheriting ctr: Cannot
PRIVATE SCOPED CTR*: Can ctr be inside private section(Yes. Single DP)

Ctr calling Hierarchy (Base Class > Derv Class Ctr)

Derived class object also have inherited properties of Base class,
and only base class constructor can properly initialize base class members

class A{
    public:
        A() { cout<<"base ctr";  }
        ~A() { cout<<"base dtr";  }
};
    
class B: public A {
    public:
        B() { cout<<"Derv ctr";  }
        ~B() { cout<<"Derv dtr";  }
};
int main(){
    B obj;
}
$ a.out
base ctr
Derv ctr
Derv dtr
Base dtr
            

Base class constructor should be defined

class A{
    public:
        A();
        ~A();
};
class B: public A {
public:
    B() { cout<<"B ctr";  }
    ~B() { cout<<"~B";  }
};
int main(){
    B obj;
}
# ./a.out
Error cannot find A::A()
                

Ctr Initializer list

In the initializer list, the order of execution takes place according to the order of declaration of member variables.

class test{
    int a; //Declared 1st
    int b; //Declared 2nd
public:
    test (int x): b(x), a(b * 2) {
        cout << b << a;       //b=10, a=garbage
    }
};
int main() {
    test obj(10);
    return 0;
}
$ ./a.exe
b = 10
a = garbage value
This is because, a is declared before b. Initialization of a will happen before b.
Since a = b*2, and b does not have value hence a is initialized to garbage.
        

Types of Constructors

1. Copy Constructor(lvalue reference)

Creates copy of object

using vi = vector <int>;
class A {
    vi a;
    void print() {
        for (decltype(auto)i:a)
            cout << i;
    }
public:
    A(vi& b):a(b) { // copy ctr (using lvalue ref). Created a copy
        cout << "\nCopy ctr\n";
        print();
    }
};
int main() {
    vi a = {1,2,3};
    A obj(a);           // Copy ctr called. Local copy remains present
    cout << "\nAfter copy ctr in main()\n";
    for(decltype(auto)i:a)
        cout << i;
}
/*
$ ./a.out
Copy ctr
1 2 3

After copy ctr in main()
1 2 3
$ 
*/
        

2. Move Constructor(rvalue reference) (Introduced in c++11)

Moves the object

using vi = vector <int>;
class A {
    vi a;
    void print() {
        for (decltype(auto)i:a)
            cout << i;
    }
public:
    //A(vi&& b):a(b)       ///////// This will make copy. Not correct move ctr ///////////
    A(vi&& b):a(move(b)) {  // move ctr (using rvalue ref).
        cout << "\nmove ctr\n";
        print();
    }
};
int main() {
    vi a = {1,2,3};
    A obj(a);           // Copy ctr called. Local copy remains present

    // move invokes a r value ref
    A obj1(move(a));            // move(a) is equal to (int &&x = a)
    cout << "\nAfter move ctr in main()\n";
    for(decltype(auto)i:a)
        cout << i;
}
/*
$ ./a.out
move ctr
1 2 3

After move ctr in main()    //Value moved not availble in main
$
*/